/*
 * Log.cpp
 *
 *  Created on: 2013-4-27
 *      Author: song
 */

#include "SmtPlug.h"
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)

int SmtPlug::LogUsrData(struct sockaddr_in &addr,HValue_t &value)
{
	uint16 cmd=value.head.cmd;
	switch (cmd)
	{

		case MSG_ADMIN_DEV_ADD:
		case MSG_ADMIN_DEV_DELETE:
		case MSG_ADMIN_USR_ADD:
		case MSG_ADMIN_USR_DELETE:
			LogUsrAdminData(addr,value,"UsrLog_Admin");
			break;



		case MSG_USR_CHANGE_PSWD:
			LogUsrModifyPswd(addr,value,"UsrLog_SetPswd");
			break;


		default:
			LogUsrDataDefault(addr,value,"UsrLog_Cmd");
			break;
	}

	return 0;
}

int SmtPlug::LogDevData(struct sockaddr_in &addr,HValue_t &value)
{
	uint16 cmd=value.head.cmd;
	switch (cmd)
	{

		case MSG_CX_ALL_VALUE:
		case MSG_UP_ALL_VALUE:
			LogDevAllValue(addr,value,"DevLog_Value");
			break;

		}
	return 0;
}

int SmtPlug::LogUsrDataDefault(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{
	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevId,Cmd) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d)",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			value.head.dst.id,
			value.head.cmd
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;
}


int SmtPlug::LogUsrAdminData(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{
	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	AdminId_t *pAdminId=(AdminId_t*)&value.adminid;

	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevIdFrom,DevIdTo,Cmd) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d)",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			pAdminId->firstId,
			pAdminId->lastId,
			value.head.cmd
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;
}


int SmtPlug::LogUsrModifyPswd(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{

	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevId,Cmd,Pswd) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,'%s')",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			value.head.dst.id,
			value.head.cmd,
			value.pswd
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
	//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;

}




int SmtPlug::LogDevAllValue(struct sockaddr_in &addr,HValue_t &value,char* sTable)
{

	char strSql[1000];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	Data_t *pData=(Data_t*)&value.data;

	//时间不对，不记
	if(value.head.tm <3600000 )
		return 0;

	uint8 doPonit[8]={0};
	for(uint8 i=0;i<8;i++)
	{
		if(pData->relay>>i & 0x01)
			doPonit[i]=1;
		else
			doPonit[i]=0;
	}

	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,DevId,DevDate,DevTime,IP,Port,UsrId,Cur,Do0) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d)",
	sTable,
	DateTime::GetNowDate().c_str(),
	DateTime::GetNowTime().c_str(),
	tv.tv_usec,
	value.head.src.id,
	DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
	DateTime::GetTmtTime((time_t)value.head.tm).c_str(),
	inet_ntoa(addr.sin_addr),
	ntohs(addr.sin_port),
	value.head.dst.id,
	pData->cur,
	doPonit[0]
	);
	//cout<<strSql<<endl;


	if(!pDevDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		cout<<"success"<<endl;


}

